package com.cloud.drore.eboss.operation.mapper;

import com.cloud.drore.eboos.common.base.MyMapper;

import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.entity.dealer.FinaceSourseDataDealerVo;
import com.cloud.drore.eboss.operation.entity.FinaceSettlementFlagOperation;
import com.cloud.drore.eboss.operation.entity.FinaceSettlementHistoryOperation;
import com.cloud.drore.eboss.operation.entity.FinaceSourseDataOperation;
import com.cloud.drore.eboss.operation.entity.po.FinaceSettlementHistoryPo;
import com.cloud.drore.eboos.common.entity.dealer.FinaceSourseDataDealerPo;
import com.cloud.drore.eboss.operation.entity.po.FinaceSourseDataOperationPo;
import com.cloud.drore.eboss.operation.entity.po.FundSettlementPo;
import com.cloud.drore.eboss.operation.entity.vo.FinaceSourseDataOperationAndSupplierVo;
import com.cloud.drore.eboss.operation.entity.vo.FundSettlementTotalVo;
import com.cloud.drore.eboss.operation.entity.vo.FundSettlementVo;
import com.cloud.drore.eboss.operation.entity.vo.SupplierSelectHistoryRecenetTimeVo;
import com.cloud.drore.eboss.operation.mapper.provider.FinaceSettlementHistoryProvider;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 浙江卓锐科技股份有限公司 版权所有 © Copyright 2018<br>
 *
 * @Description: 财务管理结算历史记录<br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/31 12:42 <br>
 * @Author: <a href="410508961@qq.com">夏凯</a>
 */
public interface FinaceSettlementHistoryMapper extends MyMapper<FinaceSettlementHistoryOperation> {

    @Select("select supplier_settlement_time,current_year_settlement_stage from eboss_supplier_settlement_info where supplier_id = ${id}")
        //根据供应商id查询当前结算期数的年份
    FinaceSettlementFlagOperation getCurrentSettlmentYear(@Param("id") Long id);

    @Update("update eboss_supplier_settlement_info set current_year_settlement_stage=${stage},supplier_settlement_time='${settlementTime}',modified_date='${date}' where supplier_id=${id}")
    int updateFinaceSettlementFlagOperation(@Param("settlementTime") String settlementTime, @Param("stage") int stage, @Param("id") Long id, @Param("date") String date);

    @SelectProvider(type = FinaceSettlementHistoryProvider.class, method = "queryFinaceSettlementHistoryList")
        //@Select("select * from eboss_finace_settlement_history where settlement_end_time<='${endTime}' and settlement_end_time>='${beginTime}'")
    List<FinaceSettlementHistoryOperation> findFinaceSettlementHistoryList(FinaceSettlementHistoryPo finaceSettlementHistoryPo, Long supplierId);

    @Select("select settlement_stage from eboss_finace_settlement_history where merchant_id = ${id}")
    List<FinaceSettlementHistoryOperation> queryStageOperationList(@Param("id") Long id);

    //查询所有已结算的供应商金额
    @Select("select sum(total_amount_should) as total_amount_should from eboss_finace_settlement_history")
    BigDecimal queryFinaceSettlementHistorySettleShould();

    //查询平台先下实际结算给供应商金额
    @Select("select sum(settlement_amount) from eboss_supplier_settlement_voucher where is_deleted=0")
    BigDecimal queryOfflineSettlementHistorySettleShould();


    //查询所有账户余额
    @Select("select sum(account_balance) as account_balance from eboss_distributor_account where is_deleted=0")
    BigDecimal queryFinaceSettlementDistriButorAccountBalance();

    //运营商资金结算数据查询
    @SelectProvider(type = FinaceSettlementHistoryProvider.class, method = "queryFundSettlementList")
    List<FundSettlementVo> queryFundSettlement(FundSettlementPo fundSettlementPo);

    //资金结算平台三个收入数据
    String queryFundSettlementTotalData = "select sum(total_amount_income) as total_amount_income,sum(total_amount_aleady) as  total_amount_aleady,\n" +
            "sum(total_amount_income-total_amount_should) as net_profit\n" +
            "from eboss_finace_settlement_history";

    @Select(queryFundSettlementTotalData)
    FundSettlementTotalVo queryFundSettlementTotalData();
    //分销商查询收入报表（远程调用）
    @SelectProvider(type = FinaceSettlementHistoryProvider.class, method = "queryFundSettlementFormDealer")
    List<FinaceSourseDataDealerVo> queryFundSettlementFormDealer(PagePo<FinaceSourseDataDealerPo> page);

    //运营商、供应商查询收入报表
    @SelectProvider(type = FinaceSettlementHistoryProvider.class, method = "queryFundSettlementFormOperation")
    List<FinaceSourseDataOperationAndSupplierVo> queryFundSettlementFormOperation(PagePo<FinaceSourseDataOperationPo> page);

    //根据id判断当前结算记录供应商是否进行过充值
    @Select("select settlement_flag from eboss_finace_settlement_history where id=${id}")
    Object queryRechargeCount(@Param("id") Long id);
    //根据供应商id判断是否进行自动结算过
    @Select("select count(1) as supplierSettlementFlag,settlement_end_time from eboss_finace_settlement_history where merchant_id=${id} order by settlement_end_time limit 1")
    SupplierSelectHistoryRecenetTimeVo querySupplierSettlementCount(@Param("id") Long id);
    //查询供应商的结算标识
    @Select("select supplier_id  as merchant_id,supplier_settlement_time as next_auto_settlement_time,settlement_type,settlement_cycle\n" +
            "from eboss_supplier_settlement_info")
    List<FundSettlementVo> querySettlementFlagInfo();

    @Select("select settlement_date from eboss_supplier_settlement_info where supplier_id=${id}")
    Date querySupplierSettlementTime(@Param("id") Long id);
    //测试查询最后一次截止时间
    @Select("select settlement_end_time from eboss_finace_settlement_history order by created_date limit 1")
    Date selectEndTime();
    //查询分销商已充值金额
    @Select("select sum(recharge_amount) as account_balance from eboss_distributor_recharge_record where is_deleted=0")
    BigDecimal queryDistriButorRechargeAlerady();
    //查询供应商产品上架的时间
    @Select("select created_date from eboss_supplier_settlement_info where supplier_id=${supplierId}")
    Date querySupplierProductGrounding(@Param("supplierId") Long supplierId);
    //查询供应商的结算info
    @Select("select  merchant_id, max(settlement_end_time) as settlement_end_time from eboss_finace_settlement_history group by merchant_id ")
    List<SupplierSelectHistoryRecenetTimeVo> querySupplierSettlementInfo();
    //维护自动结算需要的字段
    @Update("update eboss_supplier_settlement_info set settlement_state=0,settlement_date='${settlementDate}'")
    int updateSupplierSettlementInfo(@Param("settlementDate") String settlementDate);
}
